<?php

require_once CAMINHO_SERVIDOR . 'conexao.php';
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of campeonatoDAO
 *
 * @author Fernando
 */
class campeonatoDAO {

    private $con;

    function __construct() {
        self::setCon(conectar());
        mysql_query('SET CHARACTER SET utf8');
    }

    public function getCon() {
        return $this->con;
    }

    public function setCon($con) {
        $this->con = $con;
    }

    public function fecharConexao() {
        desconectar(self::getCon());
    }

    function executaQuery($query) {
        return mysql_query($query, self::getCon());
    }

    function executaQueryAll($query) {
        $posts = array();
        $result = mysql_query($query, self::getCon());

        if (mysql_num_rows($result)) {
            while ($post = mysql_fetch_assoc($result)) {
                $posts[] = $post;
            }
        }

        return $posts;
    }

    function executaQueryRow($query) {
        $result = mysql_query($query, self::getCon());
        return mysql_fetch_assoc($result);
    }

    function gravaCampeonato($nome, $ordem, $img) {
        $query = "INSERT INTO tb_campeonato (descricao,ordem, img) VALUES('" . $nome . "','" . $ordem . "','" . $img . "')";
        if (self::executaQuery($query)) {
            $result = mysql_insert_id();
        } else {
            $result = false;
        }
        return $result;
    }

    function cadastraCampeonato($descricao, $ordem, $img, $quantidadeFase) {
        $query = "INSERT INTO tb_campeonato (descricao, `status`, img, ordem, quantidade_fase)
                    VALUES ('" . $descricao . "','N','" . $img . "','" . $ordem . "','" . $quantidadeFase . "');";
        if (self::executaQuery($query)) {
            $result = mysql_insert_id();
        } else {
            $result = false;
        }
        return $result;
    }

    function alteraCampeonato($id, $nome, $ordem, $img) {
        $query = "UPDATE tb_campeonato SET descricao='" . $nome . "',ordem='" . $ordem . "', img='" . $img . "' WHERE id_campeonato=" . $id;
        if (self::executaQuery($query)) {
            $result = $id;
        } else {
            $result = false;
        }
        return $result;
    }

    function alteraCampeonatoNovo($id, $descricao, $qtd_fase, $img) {
        $query = "UPDATE tb_campeonato SET descricao='" . $descricao . "', img='" . $img . "', quantidade_fase = '" . $qtd_fase . "' WHERE id_campeonato=" . $id;
        if (self::executaQuery($query)) {
            $result = $id;
        } else {
            $result = false;
        }
        return $result;
    }

    function getCampeonatoById($id_campeonato) {
        $query = "SELECT * FROM tb_campeonato WHERE id_campeonato=" . $id_campeonato;
        $result = self::executaQueryRow($query);
        return $result;
    }

    function getIdCampeonatoByNome($nome) {
        $query = "SELECT COUNT(id_campeonato) as total FROM tb_campeonato WHERE descricao='" . $nome . "'";
        $result = self::executaQueryRow($query);
        return $result['total'];
    }

    function getCampeonatoByNome($nome) {
        $query = "SELECT * FROM tb_campeonato WHERE descricao='" . $nome . "'";
        $result = self::executaQueryRow($query);
        return $result;
    }

    function listaAllCampeonatos() {
        $query = "SELECT id_campeonato,descricao,img as img_campeonato FROM tb_campeonato WHERE status='S' ORDER BY ordem";
        $result = self::executaQueryAll($query);
        return $result;
    }

    function getUltimoCamp() {
        $query = "SELECT * FROM `tb_campeonato` ORDER BY id_campeonato DESC LIMIT 1;";
        $result = self::executaQueryRow($query);
        return $result;
    }

    function buscaOrdem() {
        $query = "SELECT MAX(ordem) AS ordem FROM `tb_campeonato`;";
        $result = self::executaQueryRow($query);
        return $result;
    }

}
